<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
jimport('joomla.application.component.helper');

/**
 * Default Model
 *
 * @package   Joomla
 * @subpackage  Fielddesigner
 * @since   1.5
 */
class JVModel extends JModel {

    public $_total = null;
    public $_pagination = null;
    public $_filter_order = 'filter_order';
    public $_filter_order_Dir = 'filter_order_Dir';
    public $_key = 'id';
    public $id = null;
    
    /**
     * Construct method
     * 
     * @return void
     */
    function __construct() {
        parent::__construct();
        $mainframe     = JFactory::getApplication();        
        $option        = $mainframe->get('scope'); 
        $array = JRequest::getVar ( 'cid', array (0 ), 'method', 'array');   
        $this->setId($array);

        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');

        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
        
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
        
        $filter_order     = $mainframe->getUserStateFromRequest(  $option.$this->_filter_order, $this->_filter_order, $this->_key, 'cmd' );
        $filter_order_Dir = $mainframe->getUserStateFromRequest( $option.$this->_filter_order_Dir, $this->_filter_order_Dir , 'asc', 'word' );
 
        $this->setState($this->_filter_order, $filter_order);
        $this->setState($this->_filter_order_Dir, $filter_order_Dir);
    }
    
    function setId($id) {
        // Set id and wipe data
        $this->_id = $id;
        $this->_data = null;
        $this->id = JRequest::getVar ($this->_key); 
    }
    
    function getFilterorder(){
         return array('order'=>$this->_filter_order,'order_Dir'=>$this->_filter_order_Dir);
    }
    
    function getId(){
         return $this->id;
    }
    
    function getKey(){
         return $this->_key;
    }

    function _buildQuery() {        
    }

    function &getData() {
        // if data hasn't already been obtained, load it
        if (empty($this->_data)) {
            $query = $this->_buildQuery();
            $this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));
        }
        return $this->_data;
    }
    function getTotal() {
        // Load the content if it doesn't already exist
        if (empty($this->_total)) {
            $query = $this->_buildQuery();
            $this->_total = $this->_getListCount($query);
        }
        return $this->_total;
    }

    function getPagination() {
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'),$this->getState('limit'));
        }
        return $this->_pagination;
    }
    
    function _buildContentOrderBy(){
        global $mainframe, $option; 
        
        $orderby = '';
        $filter_order     = $this->getState($this->_filter_order);
        $filter_order_Dir = $this->getState($this->_filter_order_Dir);

        /* Error handling is never a bad thing*/
        $orderby = ' ORDER BY ';
        if(!empty($filter_order) && !empty($filter_order_Dir) ){
                $orderby .= $filter_order.' '.$filter_order_Dir;
        }
        $orderby .= ', ordering';
        return $orderby;
    }  


    /**
     * Method to store a record
     *
     * @access	public
     * @param   array $data
     * @return	boolean	True on success
     */
    function store($data) {        
        $row = &$this->getTable();
                
        if(!$row->save($data)){
            $this->setError($this->_db->getErrorMsg());
            return false;
        }
        $key = $this->_key;
        $this->id = $row->$key;
                
        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    function delete() {
        $cids = $this->_id;                    
        $row = &$this->getTable();

        if (count($cids)) {
            foreach ($cids as $cid) {
                if (!$row->delete($cid)) {
                    $this->setError($this->_db->getErrorMsg());
                    return false;
                }
            }
        }
        return true;
    }
    
    /**
     * Method to sort record(s)
     *
     * @access	public
     * @param   int $direction
     * @return	boolean	True on success
     */
    function order($direction) {
        $cid =(int) $this->_id[0]; 
        $row =& $this->getTable();
        $row->load ( $cid ); 
                
        if (!$row->move($direction)) {
            $this->setError($this->_db->getErrorMsg()); //echo $cid.$direction; die;
            return false;
         }        
       return true;
    }    
   
   /**
     * Method to multi sort record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    function saveorder(){
        $cids = $this->_id;
        $order  = JRequest::getVar ( 'order', array (0 ), 'method', 'array');               
        $row = &$this->getTable();
        
        if (count($cids)) {
            for($i = 0, $n = count($cids); $i<$n;$i++){
                $row->load((int) $cids[$i]); 
                $row->ordering = $order[$i]; 
                if(!$row->store()){
                    $this->setError($this->_db->getErrorMsg());
                    return false;
                }
            }
        } 
         
       // Sort ordering
       $row->reorder();
       return true;
    } 
    
     /**
     * Method to publish/unpublish record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */        
    function publish (){
        $publish = JRequest::getVar('task') == 'unpublish' ? 0 : 1;
        $row =& $this->getTable();  
            
        if (!$row->publish($this->_id,$publish)) {
           $this->setError($this->_db->getErrorMsg());
           return false;
        }  
        return true;
    }                    

}
